package com.xyd.listener;


import com.xyd.Entity.User;


import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class EncodingListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

    private ServletContext Application =null;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        String path = servletContextEvent.getServletContext().getContextPath();
        System.out.println("应用程序开始,时间："+new Date().toLocaleString());
        System.out.println("应用程序开始,路径:"+path);

        Application =servletContextEvent.getServletContext();

        Set<User> onLineUsers = new HashSet<>();
        Application.setAttribute("onLineUsers",onLineUsers);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        String path = servletContextEvent.getServletContext().getContextPath();
        System.out.println("应用程序关闭,时间："+new Date().toLocaleString());
        System.out.println("应用程序关闭,时间:"+path);

        Application = servletContextEvent.getServletContext();

        Application.removeAttribute("onLineUsers");

    }

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("session创建");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("session销毁");
        HttpSession session = httpSessionEvent.getSession();
        User user = (User) session.getAttribute("user");
        Set<User> onLineUsers  = (Set<User>) Application.getAttribute("onLineUsers");
        onLineUsers.remove(user);
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("用户登录，添加session");

        HttpSession session = httpSessionBindingEvent.getSession();
        User user = (User) session.getAttribute("user");
        Set<User> onLineUsers  = (Set<User>) Application.getAttribute("onLineUsers");
        onLineUsers.add(user);
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("用户注销，移除session");

        HttpSession session = httpSessionBindingEvent.getSession();
        User user = (User) session.getAttribute("user");
        Set<User> onLineUsers  = (Set<User>) Application.getAttribute("onLineUsers");
        onLineUsers.remove(user);
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("用户替换，修改session");

        HttpSession session = httpSessionBindingEvent.getSession();
        User olduser = (User) httpSessionBindingEvent.getValue();
        User user = (User) session.getAttribute("user");
        Set<User> onLineUsers  = (Set<User>) Application.getAttribute("onLineUsers");
        onLineUsers.remove(olduser);
        onLineUsers.add(user);
    }
}
